package Tools
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.IBitmapDrawable;
	import flash.display.Sprite;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedSuperclassName;

	public class BitmapTool
	{
		public function BitmapTool()
		{
			super();
		}
		public static function getImageByData(imageData:Object):BitmapData
		{
			var image:BitmapData;
			if(imageData is Class)
			{
				image=new imageData().bitmapData;
			}
			if(imageData is BitmapData)
			{
				image=BitmapData(imageData);
			}
			if(imageData is Bitmap)
			{
				imageData=Bitmap(imageData).bitmapData;
			}
			return image;
		}
		public static function scale(imageData:Object,width:int, height:int):BitmapData
		{
			var sourceImage:BitmapData=getImageByData(imageData);
			
			if(sourceImage.width==width&&sourceImage.height==height)
			{
				return sourceImage;
			}
			
			var minWidth:Number=Math.min(sourceImage.width,width);
			var minHeight:Number=Math.min(sourceImage.height,height);
			
			var edgeH:int=Math.floor((minWidth-1)/2);
			var edgeV:int=Math.floor((minHeight-1)/2);
			
			var newImage:BitmapData = new BitmapData(width, height, true,0);
			newImage.copyPixels(sourceImage, new Rectangle(0, 0, edgeH, edgeV), new Point(0, 0));
			newImage.copyPixels(sourceImage, new Rectangle(sourceImage.width - edgeH, sourceImage.height - edgeV, edgeH, edgeV), new Point(newImage.width - edgeH, newImage.height - edgeV));
			newImage.copyPixels(sourceImage, new Rectangle(0, sourceImage.height - edgeV, edgeH, edgeV), new Point(0, newImage.height - edgeV));
			newImage.copyPixels(sourceImage, new Rectangle(sourceImage.width - edgeH, 0, edgeH, edgeV), new Point(newImage.width - edgeH, 0));
			var y:int = edgeV;
			while (y < newImage.height - edgeV) 
			{
				newImage.copyPixels(sourceImage, new Rectangle(0, edgeV, edgeH, 1), new Point(0, y));
				newImage.copyPixels(sourceImage, new Rectangle(sourceImage.width - edgeH, edgeV, edgeH, 1), new Point(newImage.width - edgeH, y));
				y++;
			}
			var x:int = edgeH;
			while (x < newImage.width - edgeH)
			{
				newImage.copyPixels(sourceImage, new Rectangle(edgeH, 0, 1, edgeV), new Point(x, 0));
				newImage.copyPixels(sourceImage, new Rectangle(edgeH, sourceImage.height - edgeV, 1, edgeV), new Point(x, newImage.height - edgeV));
				x++;
			}
			newImage.floodFill(newImage.width/2, newImage.height/2, sourceImage.getPixel32(edgeH, edgeV));
			return newImage;
		}
		public static function tint(imageData:Object,color:uint,strength:Number=1):BitmapData
		{
			var colors:Array=MathTool.splitColor(color);
			
			var colorTransform:ColorTransform=new ColorTransform();
			colorTransform.redMultiplier=strength;
			colorTransform.redOffset=colors[0];
			colorTransform.greenMultiplier=strength;
			colorTransform.greenOffset=colors[1];
			colorTransform.blueMultiplier=strength;
			colorTransform.blueOffset=colors[2];
			
			var sourceImage:BitmapData=getImageByData(imageData);
			
			var newImage:BitmapData = new BitmapData(sourceImage.width, sourceImage.height, true,0);
			
			newImage.draw(sourceImage,null,colorTransform);
			
			return newImage;
		}
		public static function cut(imageData:Object,columns:int,rows:int):Array
		{
			var sourceImage:BitmapData=getImageByData(imageData);
			
			var frames:Array=new Array();
			
			var m:int=0;
			while(m<rows)
			{
				var n:int=0;
				while(n<columns)
				{
					var index:int=m*columns+n;
					var itemWidth:Number=sourceImage.width/columns;
					var itemHeight:Number=sourceImage.height/rows;
					var bitmapData:BitmapData=new BitmapData(itemWidth,itemHeight,true,0);
					bitmapData.copyPixels(sourceImage,new Rectangle(n*itemWidth,m*itemHeight,itemWidth,itemHeight),new Point(0,0));
					var bounds:Rectangle=bitmapData.getColorBoundsRect(0xFF000000,0x00000000,false);
					if(!bounds.isEmpty())
					{
						frames[index]=bitmapData;
					}
					n++;
				}
				m++;
			}
			
			return frames;
		}
		
		public static function matrix(frames:Array,columns:int):BitmapData
		{
			var itemWidth:int=frames[0].width;
			var itemHeight:int=frames[0].height;
			
			var maxWith:Number=columns*itemWidth;
			
			var rows:int=Math.ceil(frames.length/columns);
			
			var image:BitmapData=new BitmapData(itemWidth*columns,itemHeight*rows,true,0);
			
			var m:int=0;
			while(m<rows)
			{
				var n:int=0;
				while(n<columns)
				{
					var index:int=m*columns+n;
					if(index<frames.length)
					{
						var bitmapData:BitmapData=frames[index];
						var movieClip:Sprite=new Sprite();
						movieClip.x=n*itemWidth;
						movieClip.y=m*itemHeight;
						image.draw(bitmapData,movieClip.transform.matrix);
					}
					n++;
				}
				m++;
			}
			
			return image;
		}
		//绘制图像
		public static function draw(target:BitmapData,imageData:Object, x:Number, y:Number, scaleX:Number=1, scaleY:Number=1, rotation:int=0, blendMode:String="normal"):void
		{
			var source:IBitmapDrawable;
			if(imageData is IBitmapDrawable)
			{
				source=imageData as IBitmapDrawable;
			}
			else
			{
				source=getImageByData(imageData);
			}
			var sourceImage:BitmapData=getImageByData(imageData);
			var matrixClip:Sprite=new Sprite();
			matrixClip.scaleX = scaleX;
			matrixClip.scaleY = scaleY;
			matrixClip.rotation = rotation;
			matrixClip.x = x;
			matrixClip.y = y;
			target.draw(source, matrixClip.transform.matrix, null, blendMode);
		}
		//裁切掉透明部分
		public static function tailor(imageData:Object):BitmapData
		{
			var sourceImage:BitmapData=getImageByData(imageData);
			var bounds:Rectangle=sourceImage.getColorBoundsRect(0xFF000000,0x00000000,false);
			var newImage:BitmapData=new BitmapData(bounds.width,bounds.height,true,0);
			newImage.copyPixels(sourceImage,bounds,new Point(0,0));
			return newImage;
		}
	}
}